/**
 * @author peter
 * This is a resource which implements file list, create load and save from GData documents, using server-side PHP
 */
dojo.provide("bunkai.bk_resource_gdata");
dojo.declare("bunkai.bk_resource_gdata", bk_resource,
{
	tabcontainer: "", // The dojo.tabcontainer where we'll create a new tab for ourselves
	contentpane: "",  // Our tab
	treedata: "", // The data which descries all the files we can access, after the user has logged in.
	url: "bunkai.php", // The url to the server which manages access to the GData resource
	getfilecallback: "", // Where to call back with file data, once we've got it back from the server
	getlistcallback: "", // callback for the list of files
	treecount: 0, // Used to make unique id's when logging in multiple times
	okbutton: "", // OK utton for form
    //Any initialization code would go here in the constructor. dijit._Widget and such
		
		
	constructor: function(tc)
	{
		this.tabcontainer = dijit.byId(tc);
		this.resource_id = "Google Documents"; // Defined in superclass 
	},
	
	// Called from framework, when we're selected by user
	show: function()
	{
		if(!this.contentpane)
		{
			this.contentpane = new dijit.layout.ContentPane({title: this.getName()}, document.createElement('div'));
			// Fill up with stuff
			
		}		
		// Add the whole thing
		this.tabcontainer.addChild(this.contentpane);
		
	},
	
	getName: function()
	{
		return this.resource_id;
	},
				
	// called by framework
	saveFromFramework: function(data, id)
	{
			
	},
	
	// Returns a dojo.data.ItemFileReadStore object that contains all available files for the resource,
	// possibly in a hierarchical format.
	getFileList: function(cb, fcb)
	{
		console.log("bk_resource_gdocs getFileList called");
		
		if(!this.okbutton)
		{
			console.log("Adding event handler for OK Button..");
			var b = dojo.byId(this.id+"_okbutton");		
			//console.dir(b);
			var self = this;
			dojo.connect(b, "onclick", function()
			{
				console.log("event handler reached for OK button. Now what?");
				self._handleLogin();
			});
			this.okbutton = b;
		}
		
		this.getlistcallback = cb;
		this.getfilecallback = fcb;

		if(!this.treedata)
		{
			var x = dijit.byId(this.id+"_dialog");
			//console.dir(x);
			x.show();
		}
		else
		{
			this.getlistcallback(this.treedata);
		}
	},

	// Returns the file contents of the file in the resource which corespinds to the id file_id
	getFile : function(file_id)
	{
		console.log("bk_resource_gdocs getFile called with '"+file_id+"'");
		var url = "bunkai.php";
		dojo.xhrPost(
		{
			//url: "http://localhost:8080/content.json?maxlevels=999",
			//url: "http://localhost:8080/content/mynode.json",
			url: url,
			content: {cmd: 'getDocument', docid: file_id},
			load: function(resp, args)
			{
				console.log("getDocument server returns; "+resp.length+" bytes");
				//console.dir(resp);
				self.getfilecallback(resp);
			}
		});
	},

	storeFile : function(file_id, content)
	{
		console.log("bk_resource_gdocs storeFile caleld(unimplemented)");
	},

	newFile : function(file_name)
	{

	},

	// ------------------------------ Private function
	
	_handleLogin: function ()
	{
		console.log("bk_resource_gdocs handleLogin called");
		var d = dijit.byId(this.id+"_dialog");	
		//var url = "http://localhost/bunkai-0.2/bunkai/bunkai.php";
		var url = this.url;
		var self = this;
		dojo.xhrPost(
		{
			//url: "http://localhost:8080/content.json?maxlevels=999",
			//url: "http://localhost:8080/content/mynode.json",
			url: url,
			form: 'loginform',
			load: function(resp, args)
			{
				console.log("bk_resource_gdocs handleLogin server returns; "+resp.length+" bytes");
				d.hide();
				self.treedata = eval("("+resp+")");
				self.getlistcallback(self.treedata);
			}
		});
	},

	_makeTree: function (data)
	{		
		console.log("bk_resource_gdocs makeTree called");
		var store;
		var self = this;
		try
		{
			//store = new dojo.data.ItemFileReadStore({data: fakestore});
			store = new dojo.data.ItemFileReadStore({data: data});
			//console.log("store is;");
			//console.dir(store);	
			console.log("bk_resource_gdocs creating resource tree");
			dojo.byId(this.id+"_treebox").innerHTML ="";
			var mytree = new dijit.Tree(
			{
				store: store,
				id: 'mytreeid'+(this.treecount++),
				labelAttr: "name",
				label: "Google Documents"
			}
			, document.createElement("div"));
		}
		catch(e)
		{
			console.log("bk_resource_gdocs tree error; "+e);
			debugger;
		}
		dojo.connect(mytree, "onClick", function(item)
		{
			console.log("bk_resource_gdocs tree clicked at "+item+", name "+item.name+" id "+item.id);
			//console.dir(item);
			self.getFile(item.id);
		});		
		dojo.byId(this.id+"_treebox").appendChild(mytree.domNode);		
	}
	
});